package tool.codescan.mappers;

import java.util.List;

import tool.data.TypeDefinition;


	/**
	 * @author Jamie
	 * @date 2013/11/14
	 * 
	 * Represents a type/file in a source file hierarchy.
	 * Used by {@link PackageMapper} to represent a parsed directory structure.
	 */

public class TypeNode {

	private String name;
	private PackageNode parent;
	
	public TypeNode (String name, PackageNode parentNode) {
		
		this.name = name;
		this.parent = parentNode;
		
	}
	
	
    /**
     * Checks for and returns a full (package) path to a type, if the type exists in the package(s)
     * represented by this object.
     * @param typeName - the object representing the name of the Type to look for
     * @param packageNames - the list of imports which may hint at the location of the desired type
     * @return - TypeDefinition - updated object representing the name of the parameter if any 
     *           relevant path information is found.
     *                    
     * Note: This is the 'base case' of a recursive structure, as such, this method works in concert
     *       with the method with an identical signature in {@link PackageNode}.
     */
	public TypeDefinition getPathForType(TypeDefinition typeName, List<String> packageNames) {
		
		TypeDefinition reply = typeName;
		
		// Usual case
		if (typeName.getName().equals(name)) {
			
			reply.setPath(getFullTypeName());
		
		// Inner class
		} else if (!packageNames.isEmpty() && packageNames.get(packageNames.size()-1).equals(name)) {
		    
		    // TODO - test this!
		    typeName.addInnerClassPrefix();
		    String path = getFullTypeName() + "." + name;
		    reply.setPath(path);
		    
		}
		
		return reply;
		
	}
	
	
	private String getFullTypeName() {
		
		return parent.getfullTypeName();
		
	}


	@Override
	public String toString () {
		
		return "\t\tType: " + name;
		
	}
	
}
